package com.dxj.dog.processadoption.service.impl;

import java.util.Date;
import java.util.List;

import com.dxj.common.annotation.DataScope;
import com.dxj.common.utils.DateUtils;
import com.dxj.common.utils.SecurityUtils;
import com.dxj.dog.admin.domain.DogAdmin;
import com.dxj.dog.admin.service.IDogAdminService;
import com.dxj.dog.processadoption.domain.DogAdoptionProcess;
import com.dxj.dog.processadoption.mapper.DogAdoptionProcessMapper;
import com.dxj.dog.processadoption.service.IDogAdoptionProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 领养过程Service业务层处理
 *
 * @author 王清江
 * @date 2022-03-27
 */
@Service
public class DogAdoptionProcessServiceImpl implements IDogAdoptionProcessService {
    @Resource
    private DogAdoptionProcessMapper dogAdoptionProcessMapper;

    @Resource
    private IDogAdminService dogAdminService;

    /**
     * 查询领养过程
     *
     * @param adoptionId 领养过程主键
     * @return 领养过程
     */
    @Override
    public DogAdoptionProcess selectDogAdoptionProcessByAdoptionId(Long adoptionId) {
        return dogAdoptionProcessMapper.selectDogAdoptionProcessByAdoptionId(adoptionId);
    }

    /**
     * 查询领养过程列表
     *
     * @param dogAdoptionProcess 领养过程
     * @return 领养过程
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<DogAdoptionProcess> selectDogAdoptionProcessList(DogAdoptionProcess dogAdoptionProcess) {
        return dogAdoptionProcessMapper.selectDogAdoptionProcessList(dogAdoptionProcess);
    }

    /**
     * 新增领养过程
     *
     * @param dogAdoptionProcess 领养过程
     * @return 结果
     */
    @Override
    public int insertDogAdoptionProcess(DogAdoptionProcess dogAdoptionProcess) {
        dogAdoptionProcess.setCreateTime(DateUtils.getNowDate());
        return dogAdoptionProcessMapper.insertDogAdoptionProcess(dogAdoptionProcess);
    }

    /**
     * 修改领养过程
     *
     * @param dogAdoptionProcess 领养过程
     * @return 结果
     */
    @Override
    public int updateDogAdoptionProcess(DogAdoptionProcess dogAdoptionProcess) {
        // 如果是设置成 成功被领养 会触发联动
        if ("4".equals(dogAdoptionProcess.getDogAdoptionStatus())) {
            //开始使用确认收获,dogId 传进来了
            //要做的事：1，改变状态成成功被领养，可以不用锁。
            dogAdoptionProcess.setUpdateBy(SecurityUtils.getUsername());
            dogAdoptionProcess.setUpdateTime(DateUtils.getNowDate());
            int i = dogAdoptionProcessMapper.updateDogAdoptionProcess(dogAdoptionProcess);
            //第二件事，把狗狗仓库一块改了
            DogAdmin dogAdmin = new DogAdmin();
            dogAdmin.setDogId(dogAdoptionProcess.getDogId());
            dogAdmin.setDogAdoptionStatus("3");
            dogAdmin.setDogAdoptionTime(new Date());
            dogAdmin.setVersion(dogAdminService.selectDogAdminByDogId(dogAdoptionProcess.getDogId()).getVersion());
            return i + dogAdminService.updateDogAdmin(dogAdmin);
        } else {
            return dogAdoptionProcessMapper.updateDogAdoptionProcess(dogAdoptionProcess);
        }
    }

    /**
     * 批量删除领养过程
     *
     * @param adoptionIds 需要删除的领养过程主键
     * @return 结果
     */
    @Override
    public int deleteDogAdoptionProcessByAdoptionIds(Long[] adoptionIds) {
        return dogAdoptionProcessMapper.deleteDogAdoptionProcessByAdoptionIds(adoptionIds);
    }

    /**
     * 删除领养过程信息
     *
     * @param adoptionId 领养过程主键
     * @return 结果
     */
    @Override
    public int deleteDogAdoptionProcessByAdoptionId(Long adoptionId) {
        return dogAdoptionProcessMapper.deleteDogAdoptionProcessByAdoptionId(adoptionId);
    }
}
